package com.taotao.test2.controller;


import com.taotao.test2.config.MinioClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@RestController

public class TestControllerqqq {
    @Autowired
    public void setMinioClientUtil(MinioClientUtil minioClientUtil) {
        this.minioClientUtil = minioClientUtil;
    }

    private MinioClientUtil minioClientUtil;

    @PostMapping("/upload")
    public String uploadMinio(@RequestPart MultipartFile file) throws Exception {

        //拿到图片  MultipartFile封装接受的类
        //拿到图片的名称
        String filename = file.getOriginalFilename();

        //拿到图片的 UUId + 图片类型 (解决图片重名的问题 )
        String uuid = UUID.randomUUID().toString();
        String imgType = filename.substring(filename.lastIndexOf("."));

        //图片文件的新名称 xxx/uuid.jpg   图片拼接后的名
        String fileName = uuid + imgType;

        boolean flag = minioClientUtil.putObject(fileName, file.getInputStream());


        String path = "/upload/" + fileName;

        return path;
    }

    @PostMapping("/downLoad")
    public void downLoadMinio(String url, HttpServletResponse response) throws Exception {
        // String objectUrl = minioClientUtil.getObjectUrl("a9e203f9-1bbb-4d59-8c28-3a183c064502.sql")
        String trim = url.trim();
        String path = trim.substring(trim.indexOf("/", 1), trim.length());
        minioClientUtil.getObject(path, response);

    }

}